<template>
  <a-modal
    :title="`团购商品${model ? '编辑' : '新增'}`"
    :width="800"
    :visible="visible"
    :centered="true"
    :confirmLoading="loading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id']" disabled />
        </a-form-item>
        <a-row>
          <a-col :span="12">
            <a-form-item label="名称" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-input v-decorator="['name']" placeholder="请填写名称" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="类型" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-select v-decorator="['type']" placeholder="请选择类型">
                <a-select-option :value="item.type" v-for="item in goodsKinds" :key="item.type">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="form.getFieldValue('type') !== 4">
            <a-form-item label="优惠券" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-select v-decorator="['type_id']" placeholder="请选择优惠券">
                <a-select-option :value="item.id" v-for="item in couponList" :key="item.id">
                  {{ item.type | typeFilter | nameFilter(item.discount) }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="原价" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-input-number v-decorator="['old_price']" placeholder="请填写原价" :step="0.1" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="现价" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-input-number v-decorator="['price']" placeholder="请填写现价" :step="0.1" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开始时间" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-date-picker
                v-decorator="['atime']"
                :show-time="true"
                placeholder="请选择活动开始时间"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="结束时间" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-date-picker v-decorator="['etime']" :show-time="true" placeholder="请选择活动结束时间" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="颜色尺寸" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-input v-decorator="['color_size']" placeholder="请填写颜色尺寸" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="库存" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-input-number v-decorator="['stock']" placeholder="请填写库存" :step="1" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="是否上架" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
              <a-select v-decorator="['status', { initialValue: 0 }]" placeholder="请选择是否上架">
                <a-select-option :value="1">是</a-select-option>
                <a-select-option :value="0">否</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品介绍" :labelCol="{ span: 4 }" :wrapperCol="{ span: 20 }">
              <a-textarea v-decorator="['content']" placeholder="请填写商品介绍" :auto-size="{ minRows: 3 }" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-item label="主图" style="margin-bottom:0" :labelCol="{ span: 4 }" :wrapperCol="{ span: 20 }">
              <d-upload
                v-decorator="[
                  'head_img',
                  {
                    initialValue: ''
                  }
                ]"
              >
              </d-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="内容图" style="margin-bottom:0" :labelCol="{ span: 4 }" :wrapperCol="{ span: 20 }">
              <d-upload
                v-decorator="[
                  'content_img',
                  {
                    initialValue: []
                  }
                ]"
                :multiple="true"
              >
              </d-upload>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { couponList } from '@/api/notControl'
import coupon from '@/mixins/coupon'
import formModalMixin from '@/mixins/formModalMixin'
// 表单字段
const fields = [
  'name',
  'id',
  'head_img',
  'type',
  'type_id',
  'content',
  'color_size',
  'price',
  'content_img',
  'old_price',
  'atime',
  'etime',
  'status',
  'stock'
]

export default {
  mixins: [coupon, formModalMixin],
  props: {
    addAction: {
      type: Function,
      default: () => {}
    },
    editAction: {
      type: Function,
      default: () => {}
    }
  },
  data () {
    this.fields = fields
    return {
      formLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 21 }
        }
      },
      goodsKinds: [
        {
          name: '洗衣',
          type: 1
        },
        {
          name: '家电',
          type: 2
        },
        {
          name: '家政',
          type: 3
        },
        {
          name: '商城',
          type: 4
        }
      ],
      couponList: []
    }
  },
  created () {
    couponList().then(res => {
      this.couponList = res.data
    })
  }
}
</script>
